home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.0.14 / chrome / pippki.jar / content / pippki / password.js < prev    next >
Encoding:
Text File  |  2007-02-02  |  10.2 KB  |  335 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is mozilla.org code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Bob Lord <lord@netscape.com>
  23.  *   Terry Hayes <thayes@netscape.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38. const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1";
  39. const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB;
  40. const nsIDialogParamBlock = Components.interfaces.nsIDialogParamBlock;
  41. const nsPKCS11ModuleDB = "@mozilla.org/security/pkcs11moduledb;1";
  42. const nsIPKCS11ModuleDB = Components.interfaces.nsIPKCS11ModuleDB;
  43. const nsIPKCS11Slot = Components.interfaces.nsIPKCS11Slot;
  44. const nsIPK11Token = Components.interfaces.nsIPK11Token;
  45.  
  46. var params;
  47. var tokenName="";
  48. var pw1;
  49.  
  50. function onLoad()
  51. {
  52.   document.documentElement.getButton("accept").disabled = true;
  53.  
  54.   pw1 = document.getElementById("pw1");
  55.   try {
  56.      params = window.arguments[0].QueryInterface(nsIDialogParamBlock);
  57.      tokenName = params.GetString(1);
  58.   } catch(exception) {
  59.       // this should not happen.
  60.       // previously we had self.name, but self.name was a bad idea
  61.       // as window name must be a subset of ascii, and the code was
  62.       // previously trying to assign unicode to the window's name.
  63.       // I checked all the places where we get a password prompt and
  64.       // all of them pass an argument as part of this patch.
  65.       tokenName="";
  66.   }
  67.       
  68.  
  69.   if(tokenName=="") {
  70.      var sectokdb = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
  71.      var tokenList = sectokdb.listTokens();
  72.      var enumElement;
  73.      var i=0;
  74.      var menu = document.getElementById("tokenMenu");
  75.      try {
  76.         for ( ; !tokenList.isDone(); tokenList.next()) {
  77.            enumElement = tokenList.currentItem();
  78.            var token = enumElement.QueryInterface(nsIPK11Token);
  79.            if(token.needsLogin() || !(token.needsUserInit)) {
  80.               var menuItemNode = document.createElement("menuitem");
  81.               menuItemNode.setAttribute("value", token.tokenName);
  82.               menuItemNode.setAttribute("label", token.tokenName);
  83.               menu.firstChild.appendChild(menuItemNode);
  84.               if (i == 0) {
  85.                  menu.selectedItem = menuItemNode;
  86.                  tokenName = token.tokenName;
  87.               }
  88.               i++;
  89.            }
  90.         }
  91.      }catch(exception){}
  92.   } else {
  93.     var sel = document.getElementById("tokenMenu");
  94.     sel.setAttribute("hidden", "true");
  95.     var tag = document.getElementById("tokenName");
  96.     tag.setAttribute("value",tokenName);
  97.   }
  98.           
  99.   process();
  100. }
  101.  
  102. function onMenuChange()
  103. {
  104.    //get the selected token
  105.    var list = document.getElementById("tokenMenu");
  106.    tokenName = list.value;
  107.  
  108.    process();
  109. }
  110.  
  111.  
  112. function process()
  113. {
  114.    var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
  115.    var bundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");
  116.  
  117.    // If the token is unitialized, don't use the old password box.
  118.    // Otherwise, do.
  119.  
  120.    var slot = secmoddb.findSlotByName(tokenName);
  121.    if (slot) {
  122.      var oldpwbox = document.getElementById("oldpw");
  123.      var msgBox = document.getElementById("message");
  124.      var status = slot.status;
  125.      if (status == nsIPKCS11Slot.SLOT_UNINITIALIZED
  126.          || status == nsIPKCS11Slot.SLOT_READY) {
  127.       
  128.        oldpwbox.setAttribute("hidden", "true");
  129.        msgBox.setAttribute("value", bundle.GetStringFromName("password_not_set")); 
  130.        msgBox.setAttribute("hidden", "false");
  131.  
  132.        if (status == nsIPKCS11Slot.SLOT_READY) {
  133.          oldpwbox.setAttribute("inited", "empty");
  134.        } else {
  135.          oldpwbox.setAttribute("inited", "true");
  136.        }
  137.       
  138.        // Select first password field
  139.        document.getElementById('pw1').focus();
  140.     
  141.      } else {
  142.        // Select old password field
  143.        oldpwbox.setAttribute("hidden", "false");
  144.        msgBox.setAttribute("hidden", "true");
  145.        oldpwbox.setAttribute("inited", "false");
  146.        oldpwbox.focus();
  147.      }
  148.    }
  149.  
  150.   if (params) {
  151.     // Return value 0 means "canceled"
  152.     params.SetInt(1, 0);
  153.   }
  154.   
  155.   checkPasswords();
  156. }
  157.  
  158. function onP12Load(disableOkButton)
  159. {
  160.   document.documentElement.getButton("accept").disabled = disableOkButton;
  161.   pw1 = document.getElementById("pw1");
  162.   params = window.arguments[0].QueryInterface(nsIDialogParamBlock);
  163.   // Select first password field
  164.   document.getElementById('pw1').focus();
  165. }
  166.  
  167. function setPassword()
  168. {
  169.   var pk11db = Components.classes[nsPK11TokenDB].getService(nsIPK11TokenDB);
  170.   var token = pk11db.findTokenByName(tokenName);
  171.  
  172.   var oldpwbox = document.getElementById("oldpw");
  173.   var initpw = oldpwbox.getAttribute("inited");
  174.   var bundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");
  175.   
  176.   var success = false;
  177.   
  178.   if (initpw == "false" || initpw == "empty") {
  179.     try {
  180.       var oldpw = "";
  181.       var passok = 0;
  182.       
  183.       if (initpw == "empty") {
  184.         passok = 1;
  185.       } else {
  186.         oldpw = oldpwbox.value;
  187.         passok = token.checkPassword(oldpw);
  188.       }
  189.       
  190.       if (passok) {
  191.         if (initpw == "empty" && pw1.value == "") {
  192.           // This makes no sense that we arrive here, 
  193.           // we reached a case that should have been prevented by checkPasswords.
  194.         } else {
  195.           if (pw1.value == "") {
  196.             var secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
  197.             if (secmoddb.isFIPSEnabled) {
  198.               // empty passwords are not allowed in FIPS mode
  199.               alert(bundle.GetStringFromName("pw_change2empty_in_fips_mode"));
  200.               passok = 0;
  201.             }
  202.           }
  203.           if (passok) {
  204.             token.changePassword(oldpw, pw1.value);
  205.             if (pw1.value == "") {
  206.               alert(bundle.GetStringFromName("pw_erased_ok")
  207.                     + " "
  208.                     + bundle.GetStringFromName("pw_empty_warning"));
  209.             } else {
  210.               alert(bundle.GetStringFromName("pw_change_ok")); 
  211.             }
  212.             success = true;
  213.           }
  214.         }
  215.       } else {
  216.         oldpwbox.focus();
  217.         oldpwbox.setAttribute("value", "");
  218.         alert(bundle.GetStringFromName("incorrect_pw")); 
  219.       }
  220.     } catch (e) {
  221.       alert(bundle.GetStringFromName("failed_pw_change")); 
  222.     }
  223.   } else {
  224.     token.initPassword(pw1.value);
  225.     if (pw1.value == "") {
  226.       alert(bundle.GetStringFromName("pw_not_wanted")
  227.             + " " 
  228.             + bundle.GetStringFromName("pw_empty_warning"));
  229.     }
  230.     success = true;
  231.   }
  232.  
  233.   if (success && params)
  234.     // Return value 1 means "successfully executed ok"
  235.     params.SetInt(1, 1);
  236.  
  237.   // Terminate dialog
  238.   return success;
  239. }
  240.  
  241. function getPassword()
  242. {
  243.   // grab what was entered
  244.   params.SetString(2, pw1.value);
  245.   // Return value
  246.   params.SetInt(1, 1);
  247.   // Terminate dialog
  248.   return true;
  249. }
  250.  
  251. function setP12Password()
  252. {
  253.   // grab what was entered
  254.   params.SetString(2, pw1.value);
  255.   // Return value
  256.   params.SetInt(1, 1);
  257.   // Terminate dialog
  258.   return true;
  259. }
  260.  
  261. function setPasswordStrength()
  262. {
  263. // Here is how we weigh the quality of the password
  264. // number of characters
  265. // numbers
  266. // non-alpha-numeric chars
  267. // upper and lower case characters
  268.  
  269.   var pw=document.getElementById('pw1').value;
  270. //  alert("password='" + pw +"'");
  271.  
  272. //length of the password
  273.   var pwlength=(pw.length);
  274.   if (pwlength>5)
  275.     pwlength=5;
  276.  
  277.  
  278. //use of numbers in the password
  279.   var numnumeric = pw.replace (/[0-9]/g, "");
  280.   var numeric=(pw.length - numnumeric.length);
  281.   if (numeric>3)
  282.     numeric=3;
  283.  
  284. //use of symbols in the password
  285.   var symbols = pw.replace (/\W/g, "");
  286.   var numsymbols=(pw.length - symbols.length);
  287.   if (numsymbols>3)
  288.     numsymbols=3;
  289.  
  290. //use of uppercase in the password
  291.   var numupper = pw.replace (/[A-Z]/g, "");
  292.   var upper=(pw.length - numupper.length);
  293.   if (upper>3)
  294.     upper=3;
  295.  
  296.  
  297.   var pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
  298.  
  299.   // make sure we're give a value between 0 and 100
  300.   if ( pwstrength < 0 ) {
  301.     pwstrength = 0;
  302.   }
  303.   
  304.   if ( pwstrength > 100 ) {
  305.     pwstrength = 100;
  306.   }
  307.  
  308.   var mymeter=document.getElementById('pwmeter');
  309.   mymeter.setAttribute("value",pwstrength);
  310.  
  311.   return;
  312. }
  313.  
  314. function checkPasswords()
  315. {
  316.   var pw1=document.getElementById('pw1').value;
  317.   var pw2=document.getElementById('pw2').value;
  318.  
  319.   var oldpwbox = document.getElementById("oldpw");
  320.   if (oldpwbox) {
  321.     var initpw = oldpwbox.getAttribute("inited");
  322.  
  323.     if (initpw == "empty" && pw1 == "") {
  324.       // The token has already been initialized, therefore this dialog
  325.       // was called with the intention to change the password.
  326.       // The token currently uses an empty password.
  327.       // We will not allow changing the password from empty to empty.
  328.       document.documentElement.getButton("accept").disabled = true;
  329.       return;
  330.     }
  331.   }
  332.  
  333.   document.documentElement.getButton("accept").disabled = (pw1 != pw2);
  334. }
  335.